package com.erp.programmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.PermissionMapper;
import com.erp.programmer.dao.RoleMapper;
import com.erp.programmer.domain.Permission;
import com.erp.programmer.domain.Role;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.PermissionDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.dto.RoleDto;
import com.erp.programmer.service.IPermissionService;
import com.erp.programmer.service.IRoleService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈鑫
 * @since 2021-05-23
 */
@Service
@Transactional
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    IRoleService iRoleService;
    private ResponseDto<PageDto<PermissionDto>> getAll(String msg) {
        PageDto<PermissionDto> pageDto = new PageDto<>();
        if (pageDto.getPage() ==null) {
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Permission> permissions = list();
        List<PermissionDto> permissionDtoList = CopyUtil.copyList(permissions, PermissionDto.class);
        permissionDtoList.forEach(e->{
            Role role = roleMapper.selectById(e.getRoleId());
            RoleDto roleDto = CopyUtil.copy(role, RoleDto.class);
            e.setRoleDto(roleDto);
        });
        PageInfo<Permission> pageInfo = new PageInfo<>(permissions);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(permissionDtoList);
        return ResponseDto.successByMsg(pageDto,msg);
    }
    /**
     * 查询权限信息
     */
    @Override
    public ResponseDto<PageDto<PermissionDto>> getPermission(PageDto<PermissionDto> pageDto) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        if("name".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据权限名称搜索
            queryWrapper.like("name",pageDto.getSearchContent());
        }
        if("roleId".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据权限名称搜索
            queryWrapper.eq("role_id",pageDto.getSearchContent());
        }
        if (pageDto.getPage() ==null) {
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Permission> permissions = permissionMapper.selectList(queryWrapper);
        List<PermissionDto> permissionDtoList = CopyUtil.copyList(permissions, PermissionDto.class);

        permissionDtoList.forEach(e->{
            Role role = roleMapper.selectById(e.getRoleId());
            RoleDto roleDto = CopyUtil.copy(role, RoleDto.class);
            e.setRoleDto(roleDto);
        });

        PageInfo<Permission> pageInfo = new PageInfo<>(permissions);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(permissionDtoList);
        return ResponseDto.success(pageDto);
    }

    /**
     * 添加权限信息
     */
    @Override
    public ResponseDto<PageDto<PermissionDto>> insertPermission(PermissionDto permissionDto ) {
        if (iRoleService.getById(permissionDto.getRoleId())==null) {
            return ResponseDto.errorByMsg(CodeMsg.ROLE_NOT_EXIST);
        }
        Permission permission = CopyUtil.copy(permissionDto, Permission.class);
        if (permissionMapper.insert(permission)<1) {
            return ResponseDto.errorByMsg(CodeMsg.AUTHORITY_ADD_ERROR);
        }
        return getAll("添加成功");
    }

    /**
     * 修改权限信息
     */
    @Override
    public ResponseDto<PageDto<PermissionDto>> updatePermission(PermissionDto permissionDto) {
        if (iRoleService.getById(permissionDto.getRoleId())==null) {
            return ResponseDto.errorByMsg(CodeMsg.ROLE_NOT_EXIST);
        }
        Permission permission = CopyUtil.copy(permissionDto, Permission.class);
        if (permissionMapper.updateById(permission)<1) {
            return ResponseDto.errorByMsg(CodeMsg.AUTHORITY_EDIT_ERROR);
        }
        List<PermissionDto> permissionDtoList = CopyUtil.copyList(list(), PermissionDto.class);
        return getAll("修改成功");
    }

    @Override
    public ResponseDto<PageDto<PermissionDto>> deletePermission(PermissionDto permissionDto) {
        permissionMapper.deleteById(permissionDto.getId());
        return getAll("删除成功");
    }

    @Override
    public ResponseDto<List<PermissionDto>> getPermissionByToken(String RoleId) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",RoleId);
        List<Permission> permissionList = permissionMapper.selectList(queryWrapper);
        List<PermissionDto> permissionDtoList = CopyUtil.copyList(permissionList, PermissionDto.class);
        return ResponseDto.success(permissionDtoList);
    }


}
